/* 
 * 
 * Copyright 2011 The University of Adelaide
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *  
 */

package au.org.aekos.ands.global.parse;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import au.org.aekos.ands.global.Schema.EntityType;
import au.org.ands.standards.rifCs.registryObjects.ElectronicAddressType;
import au.org.ands.standards.rifCs.registryObjects.LocationType;
import au.org.ands.standards.rifCs.registryObjects.LocationType.Address;

public class LocationParser extends PropertyParser<LocationType> {
	
	public LocationParser(Properties properties) { super(LocationType.class, properties); }
	
	@Override
	public List<LocationType> parse(EntityType type) {
		return parse(type, null);
	}
		
	@Override
	public List<LocationType> parse(EntityType type, String identifier) {
		List<LocationType> list = new ArrayList<LocationType>();
		LocationType location = LocationType.Factory.newInstance();
		list.add(location);
		
		for (Object object : properties.keySet()) {
			String key = (String)object;
			
			String identifierPrefix = "";
			if (identifier != null) {
				identifierPrefix = identifier + ".";
			}
			
			String prefix = type.toString() + "." + identifierPrefix + "location";
			
			if (key.startsWith(prefix)) {
				List<String> tokens = split(key);
				int typeIndex = tokens.size() - 1;
				
				Address address = location.addNewAddress();
				ElectronicAddressType electronic = address.addNewElectronic();
				electronic.setType(tokens.get(typeIndex));
				electronic.setValue(prop(key));
			}
		}
		
		return list;
	}
}
